<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
  Author: arv@google.com (Erik Arvidsson)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.events.OnlineListener</title>
<script src="../base.js"></script>
<script>

goog.require('goog.events.BrowserFeature');
goog.require('goog.events.OnlineHandler');
goog.require('goog.net.NetworkStatusMonitor');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.PropertyReplacer');

</script>
</head>
<body>
<script>

var stubs = new goog.testing.PropertyReplacer();
var clock = new goog.testing.MockClock();
var online = true;
var onlineCount;
var offlineCount;

function listenToEvents(oh) {
  onlineCount = 0;
  offlineCount = 0;

  goog.events.listen(oh, goog.net.NetworkStatusMonitor.EventType.ONLINE,
                     function(e) {
                       assertTrue(oh.isOnline());
                       onlineCount++;
                     });
  goog.events.listen(oh, goog.net.NetworkStatusMonitor.EventType.OFFLINE,
                     function(e) {
                       assertFalse(oh.isOnline());
                       offlineCount++;
                     });
}

function setUp() {
  stubs.set(goog.events.OnlineHandler.prototype, 'isOnline', function() {
    return online;
  });
}

function tearDown() {
  stubs.reset();
  clock.uninstall();
}

function testConstructAndDispose() {
  var oh = new goog.events.OnlineHandler();
  oh.dispose();
}

function testNoOnlineProperty() {
  stubs.set(goog.events.BrowserFeature,
      'HAS_NAVIGATOR_ONLINE_PROPERTY', false);
  stubs.set(goog.events.EventHandler.prototype, 'listen',
      goog.testing.recordFunction());

  var oh = new goog.events.OnlineHandler();

  assertEquals(0, oh.eventHandler_.listen.getCallCount());

  oh.dispose();
}

function testNonHtml5() {
  clock.install();
  stubs.set(goog.events.BrowserFeature,
      'HAS_HTML5_NETWORK_EVENT_SUPPORT', false);

  var oh = new goog.events.OnlineHandler();
  listenToEvents(oh);

  clock.tick(500);
  online = false;
  clock.tick(500);

  assertEquals(0, onlineCount);
  assertEquals(1, offlineCount);

  online = true;
  clock.tick(500);

  assertEquals(1, onlineCount);
  assertEquals(1, offlineCount);

  oh.dispose();
  clock.dispose();
}

function testHtml5() {
  stubs.set(goog.events.BrowserFeature,
      'HAS_HTML5_NETWORK_EVENT_SUPPORT', true);

  // Test for browsers that fire network events on document.body.
  stubs.set(goog.events.BrowserFeature,
      'HTML5_NETWORK_EVENTS_FIRE_ON_BODY', true);

  var oh = new goog.events.OnlineHandler();
  listenToEvents(oh);

  online = false;
  var e = new goog.events.Event('offline');
  goog.events.fireListeners(document.body, e.type, false, e);

  assertEquals(0, onlineCount);
  assertEquals(1, offlineCount);

  online = true;
  e = new goog.events.Event('online');
  goog.events.fireListeners(document.body, e.type, false, e);

  assertEquals(1, onlineCount);
  assertEquals(1, offlineCount);

  oh.dispose();

  // Test for browsers that fire network events on window.
  stubs.set(goog.events.BrowserFeature,
      'HTML5_NETWORK_EVENTS_FIRE_ON_BODY', false);

  oh = new goog.events.OnlineHandler();
  listenToEvents(oh);

  online = false;
  e = new goog.events.Event('offline');
  goog.events.fireListeners(window, e.type, false, e);

  assertEquals(0, onlineCount);
  assertEquals(1, offlineCount);

  online = true;
  e = new goog.events.Event('online');
  goog.events.fireListeners(window, e.type, false, e);

  assertEquals(1, onlineCount);
  assertEquals(1, offlineCount);

  oh.dispose();
}

</script>
</body>
</html>
